package com.google.coffeeshop.gui.desktop.orderpayment;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import com.google.coffeeshop.business.OrderBusiness;
import com.google.coffeeshop.entity.OrderDTO;
import com.google.coffeeshop.entity.SeatDTO;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class SeatOverviewPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private JTextField tfSeatName;
	private JTextField tfEmployeeName;
	private JTextField tfTotalAmount;
	private ItemTable itemTable;
	public SeatOverviewPanel() {
		setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),}));
		
		JLabel lblSeatName = new JLabel("Seat Name");
		add(lblSeatName, "2, 2, right, default");
		
		tfSeatName = new JTextField();
		tfSeatName.setEditable(false);
		add(tfSeatName, "4, 2, fill, default");
		tfSeatName.setColumns(10);
		
		JLabel lblEmployeeName = new JLabel("Employee Name");
		add(lblEmployeeName, "2, 4, right, default");
		
		tfEmployeeName = new JTextField();
		tfEmployeeName.setEditable(false);
		add(tfEmployeeName, "4, 4, fill, default");
		tfEmployeeName.setColumns(10);
		
		JLabel lblTotalAmount = new JLabel("Total Amount");
		add(lblTotalAmount, "2, 6, right, default");
		
		tfTotalAmount = new JTextField();
		tfTotalAmount.setEditable(false);
		add(tfTotalAmount, "4, 6, fill, top");
		tfTotalAmount.setColumns(10);
		
		itemTable = new ItemTable();
		add(new JScrollPane(itemTable), "2, 8, 3, 1, fill, fill");
	}
	
	private void resetAllFields() {
		tfSeatName.setText("");
		tfEmployeeName.setText("");
		tfTotalAmount.setText("");
		itemTable.removeAllRow();
	}
	
	public void showOnGUI(SeatDTO seat) {
		if (tfSeatName.getText().equals(seat.getSeatName())) {
			return;
		}
		
		resetAllFields();
		
		tfSeatName.setText(seat.getSeatName());
		OrderDTO order = OrderBusiness.findOrder(seat.getId());
		if (order != null) {
			tfEmployeeName.setText(order.getEmployee().getFullName());
			tfTotalAmount.setText(String.valueOf(order.getAmount()));
			itemTable.addNewRows(order.getOrderLines());
		}
	}

}
